<?php

namespace app\adm\controller;

class User extends Base
{
	// 玩家列表
	public function index()
	{
		if (is_post()) {
			$act = input('get.act');
			switch ($act) {
				case 'status':
					return model('User')->changeStatus(input('post.uid'));
					break;
				case 'reset':
					return model('User')->resetPwd(input('post.uid'));
					break;
				case 'acc':
					$post = input('post.');
					return model('User')->updateUserAcc($post['uid'], $post['num'], $post['type'], session('ad_name') . '系统调整');
					break;
				case 'get':
					$uid = input('post.uid');
					return db('user')->where('uid', $uid)->find();
					break;
				case 'edit':
					$post = input('post.');
					return model('user')->editInfo($post, $post['uid']);
					break;
				case 'add':
					$post = input('post.');
					$post['pwd'] = $post['s_pwd'] = '123456';
					$validate = validate('Vuser');
			        $rst = $validate->scene('addUser')->check($post);
			        if (! $rst) {
			            $this->error($validate->getError());
			        } else {
			        	return model('user')->addInfo($post);
			        }
					break;
				default:
					break;
			}
		}

		$map = [];
		$order = '';
		if (is_get()) {
			if (input('get.keywords')) {
				$map['name|tel'] = ['like','%'.input('get.keywords').'%'];
			}
			if (is_numeric(input('get.status'))) {
				$map['status'] = input('get.status');
			}

			if (input('get.pid')) {
				$map['pid'] = input('get.pid');
			}

			if (input('get.province')) {
				$map['province'] = input('get.province');
			}

			if (input('get.city')) {
				$map['city'] = input('get.city');
			}

			if (input('get.order')) {
				$order = input('get.order').' DESC';
			} else {
				$order = 'user.create_time DESC';
			}
		}

		$this->assign('province',db('cn_area')->where('is_p', 1)->select());
		$list = model('User')->getList($map);
		$this->assign('list',$list);
		return $this->fetch();
	}

	// 商家列表
	public function shop()
	{
		if (is_post()) {

		}

		$map['is_shop'] = 1;
		$order = '';
		if (is_get()) {
			if (input('get.keywords')) {
				$map['name|tel'] = ['like','%'.input('get.keywords').'%'];
			}
			if (is_numeric(input('get.status'))) {
				$map['status'] = input('get.status');
			}

			if (input('get.pid')) {
				$map['pid'] = input('get.pid');
			}

			if (input('get.province')) {
				$map['province'] = input('get.province');
			}

			if (input('get.city')) {
				$map['city'] = input('get.city');
			}

			if (input('get.order')) {
				$order = input('get.order').' DESC';
			} else {
				$order = 'user.create_time DESC';
			}
		}

		$list = model('User')->getList($map);

		$this->assign('list', $list);

		return $this->fetch();
	}


	// 会员账户明细
	public function userAcclog()
	{
		$map = [];
		if (is_get()) {
			if (input('get.uid')) {
				$map['user_account_records.uid'] = input('get.uid');
			}
			if (input('get.acc_type')) {
				$map['acc_type'] = input('get.acc_type');
			}
			if (input('get.keywords')) {
				$keywords = input('get.keywords');
				$map['name|tel'] = ['like', '%'.$keywords.'%'];
			}
		}

		$list = \think\Db::view('user_account_records', '*')
			->view('user', 'name,tel', 'user_account_records.uid=user.uid')
			->where($map)
			->order('create_time DESC')
			->paginate();

		$this->assign('list',$list);
		return $this->fetch();
	}

}